<?php
/*
	收入明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$type=intval($_RQ['type']);
	$params=array('status'=>1);
	$params['uniacid']=$_SESSION['uniacid'];
	$params2=array('bao_time OR'=>'bao_time>0 or ding_time>0','uniacid'=>$_SESSION['uniacid']);
	if(!$type){
		$params['jbstoreid']=$_SESSION['storeid'];
	}else{
		$params['koubei_storeid']=$_SESSION['storeid'];
		$workerids=pdo_getall('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$_SESSION['storeid']),'id','id');
		if($workerids){
			$workerids=array_keys($workerids);
		}else{
			$workerids=-1;
		}
		$params['workerheid']=$workerids;
	}
	$return['params']['type']=$type;
	if($_RQ['storeid']){
		$params['storeid']=$_RQ['storeid'];
		$params2['storeid']=$_RQ['storeid'];
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['cardno']){
		$params2['cardno like']='%'.trim($_RQ['cardno']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['cardno']=$_RQ['cardno'];
	}
	if($_RQ['mstatus']){
		$params2['status']=$_RQ['mstatus'];
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['cardno']=$_RQ['cardno'];
	}
	if($_RQ['jiao_day']){
		$params['jiao_day']=$_RQ['jiao_day'];
		$return['params']['jiao_day']=$_RQ['jiao_day'];
	}
	if($_RQ['zhanghu']){
		$params['zhanghu']=$_RQ['zhanghu'];
		$return['params']['zhanghu']=$_RQ['zhanghu'];
	}
	//创建时间
	if($_RQ['jiao_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['jiao_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['jiao_time']=$time_range;
		$params['jiao_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$list=pdo_getslice('jy_yxw_infee_log',$params,array($pindex,$psize),$total,$fields,'','jiao_time desc, id desc');
	if($list){
		$fee=$fee2=$fee3=$jiaobenfee=$zufee=$peifee=$zhaofee=0;
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['memberid']);
			$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']),array('title','status','cardno','worker5id','worker6id'));
			$item['cardno']=$member['cardno'];
			$item['mstatus']=$member['status'];
			$item['zhanghu']=$item['zhanghu']?:'公户';
			$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
			$item['worker5title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker5id']),'title');
			$item['worker6title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker6id']),'title');
			$item['createtime']=date('Y-m-d',$item['createtime']);
			if($item['type']==1){
				$item['fee']=$item['fee']+$item['bufee'];
				//$item['fee']=0;
			}elseif($item['type']==2){
				$item['fee2']=$item['fee'];
				//$item['fee']=0;
			}elseif($item['type']==3){
				$item['fee3']=$item['fee'];
				//$item['fee']=0;
			}
			$fee +=$item['fee'];
			$fee2 +=$item['fee2'];
			$fee3 +=$item['fee3'];
			$jiaobenfee +=$item['jiaobenfee'];
			$zufee +=$item['zufee'];
			$peifee +=$item['peifee'];
			$zhaofee +=$item['zhaofee'];
		}
		$list[]=array('storetitle'=>'合计','fee'=>$fee,'fee2'=>$fee2,'fee3'=>$fee3,'jiaobenfee'=>$jiaobenfee,'zufee'=>$zufee,'peifee'=>$peifee,'zhaofee'=>$zhaofee);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：收入主要包括金额（包含单费+补贴）(定金	单费	退出收费)入账！','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'storetitle','title'=>'招生门店','type'=>'string'),
			array('name'=>'member','title'=>'姓名','type'=>'member'),
			array('name'=>'cardno','title'=>'身份证号','type'=>'string'),
			array('name'=>'createtime','title'=>'申请日期','type'=>'string'),
			array('name'=>'mstatus','title'=>'状态','type'=>'radio2','options'=>'mstatus'),
			array('name'=>'type','title'=>'收费类型','type'=>'radio2','options'=>'type'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'fee3','title'=>'退出收费','type'=>'string'),
			array('name'=>'pay_type','title'=>'支费方式','type'=>'radio2','options'=>'pay_types'),
			array('name'=>'daipay_type','title'=>'贷款渠道','type'=>'radio2','options'=>'daipay_types','show'=>array('pay_type'=>array(2,3))),
			array('name'=>'jiao_day','title'=>'交费日期','type'=>'string'),
			array('name'=>'zhanghu','title'=>'到账账户','type'=>'string'),
			array('name'=>'worker6title','title'=>'销售员','type'=>'string'),
			array('name'=>'worker5title','title'=>'电客','type'=>'string'),
			array('name'=>'jiaobenfee','title'=>'售后成本','type'=>'string'),
			array('name'=>'zufee','title'=>'著作权','type'=>'string'),
			array('name'=>'peifee','title'=>'培训费','type'=>'string'),
			array('name'=>'zhaofee','title'=>'招生费','type'=>'string'),
		),'batch'=>array(
			//array('type'=>'href','url'=>'/role14.in/export/','title'=>'选择月份导出'),
		)),
		'search'=>array(
			array('name'=>'storeid','width'=>10,'title'=>'招生门店','type'=>'select','options'=>'stores'),
			array('name'=>'title','width'=>10,'title'=>'姓名','type'=>'string'),
			array('name'=>'cardno','width'=>10,'title'=>'身份证号','type'=>'string'),
			//array('name'=>'mstatus','width'=>10,'title'=>'状态','type'=>'radio','options'=>'mstatus'),
			//array('name'=>'jiao_day','width'=>10,'title'=>'交费日期','type'=>'time','time_type'=>'day'),
			array('name'=>'zhanghu','width'=>10,'title'=>'到账账户','type'=>'string'),
			array('name'=>'jiao_time','width'=>15,'title'=>'交费日期','type'=>'time_range'),
		)
	);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['stores']=$this->getall('store',$params,array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['type']=$this->infee_type;
	$return['mstatus']=$this->member_status;
	$return['pay_types']=$this->pay_types;
	$return['daipay_types']=$this->daipay_types;
	exi($return);
}
if($_RQ['op']=='exportold'){
	$return['item']=array();	
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'按月导出数据',
	);
	$htmls['edit']=array(
		array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
		array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
	);
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	if(!$post['year']){
		exi('请选择年份！','error');
	}
	if(!$post['month']){
		exi('请选择月份！','error');
	}
	$lasttime=strtotime($post['year'].'-'.$post['month'].'-01');
	$url=download_url('jy_yxw/role14.in/export/lasttime='.$lasttime);
	exi('点确定开始下载','','',$url);
}
if($_RQ['op']=='export'){
	$return['params']=array();
	$params=array('status'=>1,'jbstoreid'=>$_SESSION['storeid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$params2=array('bao_time OR'=>'bao_time>0 or ding_time>0','uniacid'=>$_SESSION['uniacid']);
	if($_RQ['storeid']){
		$params['storeid']=$_RQ['storeid'];
		$params2['storeid']=$_RQ['storeid'];
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['cardno']){
		$params2['cardno like']='%'.trim($_RQ['cardno']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['cardno']=$_RQ['cardno'];
	}
	if($_RQ['mstatus']){
		$params2['status']=$_RQ['mstatus'];
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['cardno']=$_RQ['cardno'];
	}
	if($_RQ['zhanghu']){
		$params['zhanghu']=$_RQ['zhanghu'];
		$return['params']['zhanghu']=$_RQ['zhanghu'];
	}
	//创建时间
	if($_RQ['jiao_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['jiao_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['jiao_time']=$time_range;
		$params['jiao_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_yxw_infee_log',$params,$fields);
	if($list){
		$fee=$fee2=$fee3=$jiaobenfee=$zufee=$peifee=$zhaofee=0;
		foreach($list as &$item){
			$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']),array('title','status','cardno','worker5id','worker6id'));
			$item['title']=$member['title'];
			$item['cardno']=$member['cardno'];
			$item['mstatus']=$this->member_status[$member['status']];
			$item['zhanghu']=$item['zhanghu']?:'公户';
			$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
			$item['worker5title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker5id']),'title');
			$item['worker6title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker6id']),'title');
			$item['createtime']=date('Y-m-d',$item['createtime']);
			if($item['type']==1){
				$item['fee']=$item['fee']+$item['bufee'];
				//$item['fee']=0;
			}elseif($item['type']==2){
				$item['fee2']=$item['fee'];
				//$item['fee']=0;
			}elseif($item['type']==3){
				$item['fee3']=$item['fee'];
				//$item['fee']=0;
			}
			$item['pay_type']=$this->pay_types[$item['pay_type']];
			$item['daipay_type']=$this->daipay_types[$item['daipay_type']];
			$item['type']=$this->infee_type[$item['type']];
			$fee +=$item['fee'];
			$fee2 +=$item['fee2'];
			$fee3 +=$item['fee3'];
			$jiaobenfee +=$item['jiaobenfee'];
			$zufee +=$item['zufee'];
			$peifee +=$item['peifee'];
			$zhaofee +=$item['zhaofee'];
		}
		$list[]=array('storetitle'=>'合计','fee'=>$fee,'fee2'=>$fee2,'fee3'=>$fee3,'jiaobenfee'=>$jiaobenfee,'zufee'=>$zufee,'peifee'=>$peifee,'zhaofee'=>$zhaofee);
	}
	$header=array(
		'storetitle'=>'所属门店',
		'title'=>'姓名',
		'cardno'=>'身份证号',
		'createtime'=>'申请日期',
		'mstatus'=>'状态',
		'type'=>'收费类型',
		'fee2'=>'定金',
		'fee'=>'单费',
		'fee3'=>'退出收费',
		'pay_type'=>'支费方式',
		'daipay_type'=>'贷款渠道',
		'jiao_day'=>'交费日期',
		'zhanghu'=>'到账账户',
		'worker6title'=>'销售员',
		'worker5title'=>'电客',
		'jiaobenfee'=>'售后成本',
		'zufee'=>'著作权',
		'peifee'=>'培训费',
		'zhaofee'=>'招生费',
	);
	model('core/tools')->export2excel($header,$list,'收入明细');
}